<?php

class UserAction extends CommonAction {
    
    /**
     * 显示所有人员列表
     * */
    function index(){
        $list = M('User')->order('id asc')->select();
        $this->assign('list' , $list);
        $this->display();
    }
    
    /**
     * 停用/启用 账号
     * */
    function deny_accept_account(){
        $data['id'] = $_REQUEST['id'];
        $data['status'] = $_REQUEST['status'];
        // 更新数据
		$list=M('User')->save ($data);
		if (false !== $list) {
			//成功提示
			$this->successAjax ('修改成功!');
		} else {
			//错误提示
			$this->errorAjax ('修改失败!');
		}
    }
    
    /**
     * 重置密码为123456
     * */
    function reset_password(){
        $data['id'] = $_REQUEST['id'];
        $data['password'] = md5('123456');
        $list=M('User')->save ($data);
		if (false !== $list) {
			//成功提示
			$this->successAjax ('密码重置为 : 123456.');
		} else {
			//错误提示
			$this->errorAjax ('重置失败!');
		}
    }
    
    //显示密码修改对话框
    function password(){
        $this->display();
    }
    
    /**
     * 只能修改自己的密码
     * */
    function change_password(){
        if(empty($_POST['old_password'])) {
			$this->errorAjax('原始密码必须！');
		}else if (empty($_POST['new_password'])){
			$this->errorAjax('新密码必须！');
		}else if (empty($_POST['re_new_password'])){
			$this->errorAjax('确认密码必须！');
		}else if(strcmp($_POST['new_password'] , $_POST['re_new_password']) != 0){
            $this->errorAjax('新密码与确认密码不相同！');
		}
        
        $user = M('User')->find($_SESSION[C('USER_AUTH_KEY')]);
        if(isset($user) && $user['password'] == md5($_POST['old_password'])){
            $data['id'] = $user['id'];
            $data['password'] = md5($_POST['new_password']);
            M('User')->save($data);
            $this->successAjax('密码修改成功!');
        }else{
            $this->errorAjax('原始密码错误！');
        }
    }
    
    /**
     * 用户个人对自己信息的修改
     * */
    function profile(){
       
		$model = M ('User');
		$id = $_SESSION [C ( 'USER_AUTH_KEY' )];
		$vo = $model->getById ( $id );
		$this->assign ( 'vo', $vo );
		$this->display ();
    }
    
    function update_profile(){
        if($_SESSION [C ( 'USER_AUTH_KEY' )] != $_REQUEST['id']){
            //错误提示
			$this->errorAjax ('非法操作!');
        }
		$model = D ( 'User');
		if (false === $model->create ()) {
			$this->errorAjax ( $model->getError () );
		}
		// 更新数据
		$list=$model->save ();
		if (false !== $list) {
			//成功提示
			$this->successAjax ('编辑成功!');
		} else {
			//错误提示
			$this->errorAjax ('编辑失败!');
		}
    }
    
	
}